---
title: "Faces of the State"
author: "Katren Rogers & Johannes Lindvall"
output: html_document
---

### `r as.character(Sys.Date())`

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library("taRifx")
library("ggplot2")
library("dplyr")
library("doBy")
library("directlabels")
library("modeest")
library("ggplot2")
library("ggthemes")
library("extrafont")
library("haven")
library("ggpubr")
loadfonts()

```

```{r import data, include=FALSE}
df <- read_dta("./Merged dataset.dta")
df <- replace(df, df==-99, NA)
df <- replace(df, df=="United Kingdom", "England")
df$year <- destring(df$year)
df$group <- destring(df$group)
df$situation <- destring(df$situation)
df$some <- destring(df$some)
df$discretion <- destring(df$discretion)
df$level <- destring(df$level)
df$church<- destring(df$church)
df$voluntary <- destring(df$voluntary)
df$cashkind <- destring(df$cashkind)

df2<-df %>% 
  filter(situation!=11|6)
df2 <- mutate(df2, newsit = ifelse(df2$situation==15, 1, ifelse(df2$situation==13, 2, ifelse(df2$situation==1, 3, ifelse(df2$situation==14, 4, ifelse(df2$situation==10, 5, ifelse(df2$situation==8, 6, ifelse(df2$situation==4, 7, ifelse(df2$situation==3, 8, ifelse(df2$situation==2, 9, ifelse(df2$situation==12, 10, ifelse(df2$situation==5, 11, ifelse(df2$situation==7, 12, ifelse(df2$situation==9, 13, NA))))))))))))))

newsitlabs <- c("Poor/dangerous work conditions", "Too old to work",  "Unemployed",  "Widowed", "Has child born out of wedlock", "Pregnant",  "Disabled", "Mentally ill", "Physically ill", "Has a school-age child", "Victim of crime", "Party in civil legal proceeding", "Registering birth/death")

```

## Figure 1

```{r finding 1A, echo=FALSE, warning=FALSE}
fig1labs <- c("Middle, All", "Middle, Subgroup", "Poor, All", "Poor, Subgroup", "Rich, All", "Rich, Subgroup")

plotdata <-  summaryBy(some ~ cname + year + newsit + group, df2, FUN=c(mean), na.rm=TRUE)

emptyrow <- summaryBy(some ~ cname + year + newsit, df2, FUN=c(mean), na.rm=TRUE)
emptyrow<-mutate(emptyrow, group = 3)
emptyrow$some.mean <- 3

plotdata<-rbind(plotdata, emptyrow)
plotdata <-plotdata[order(plotdata$cname, plotdata$year, plotdata$newsit, plotdata$group),]
plotdata$plotid <- with(plotdata, ave(newsit, year, FUN = seq_along))

plotdata$some.mean <- replace(plotdata$some.mean, plotdata$some.mean==0.5, 0)
plotdata$some.mean[is.na(plotdata$some.mean)] <- 2
plotdata$some.mean <- factor(plotdata$some.mean,levels = c(0,1,2,3), labels = c("All", "Subgroup", "", ""))
plotdata$group <- factor(plotdata$group,levels = c(2,1,0,3), labels = c("Rich", "Middle", "Poor", ""))

## Manual work. In order to get continuous lines we need to introduce an additional observations where the line shifts from dotted to solid and where there are 'gaps'. The extra observations I add to plotdata. The gaps I plot as a separate white line geom (in the Sweden plot).

extraobs <- data.frame(c("England", "England", "England", "England", "England", "Sweden", "Sweden", "Sweden", "Sweden", "Sweden", "Sweden"),
                       c(1999, 1999, 1959, 1959, 1959, 1979, 1979, 1999, 1999, 1999, 1919),
                       c(1, 1, 2, 7, 9, 1, 1, 4, 4, 4, 3),
                       c(1, 0, 1, 1, 1, 1, 0, 2, 1, 0, 0),
                       c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
                       c(2, 1, 6, 26, 34, 58, 57, 71, 70, 69, 65))
names(extraobs) <-c("cname", "year", "newsit", "group", "some.mean","plotid")
extraobs$some.mean <- factor(extraobs$some.mean,levels = c(0,1,2,3), labels = c("All", "Subgroup", "", ""))
extraobs$group <- factor(extraobs$group,levels = c(2,1,0,3), labels = c("Rich", "Middle", "Poor", ""))
plotdata<-rbind(plotdata, extraobs)

gapdata <- data.frame(c(1960, 1980, 2000, 1880, 1900, 1920, 1940, 1960), c(69, 69, 69, 91, 91, 91, 91, 91))
names(gapdata) <-c("year", "plotid")

ENG_1a <- ggplot() + geom_line(data=subset(plotdata, cname=="England" & some.mean=="All"), aes(year, plotid, group=plotid, color=interaction(group, some.mean), linetype=interaction(group,some.mean))) + geom_line(data=subset(plotdata, cname=="England" & some.mean=="Subgroup"), aes(year, plotid, group=plotid, color=interaction(group, some.mean), linetype=interaction(group,some.mean))) + theme_classic(base_family="Gill Sans") + scale_y_continuous(name="", breaks=c(2,6,10,14,18,22,26,30,34,38,42,46,50), labels=newsitlabs, limits=c(1, 52),) + scale_x_continuous(name="\n(A) England \n", breaks=seq(1800, 2020, by = 20)) +
    scale_color_manual(name="Income group & coverage", 
                       labels = fig1labs,
                        values = c("black", "black", "#C10534", "#C10534", "#3E647D", "#3E647D")) + 
    scale_linetype_manual(name="Income group & coverage", 
                     labels = fig1labs,  
                     values=c("solid", "dashed", "solid", "dashed", "solid", "dashed")) + 
  theme(legend.position="bottom", legend.title = element_text(size = 14), legend.text = element_text(size = 14), axis.text.y = element_text(size = 16), axis.text.x = element_text(size = 14), axis.title.x = element_text(size = 22)) 

SWE_1a <- ggplot() + geom_line(data=subset(plotdata, cname=="Sweden" & some.mean=="All"), aes(year, plotid, group=plotid, color=interaction(group, some.mean), linetype=interaction(group,some.mean))) + geom_line(data=gapdata, aes(year, plotid, group=plotid), color="white", show.legend=F, size=2) +  geom_line(data=subset(plotdata, cname=="Sweden" & some.mean=="Subgroup"), aes(year, plotid, group=plotid, color=interaction(group, some.mean), linetype=interaction(group,some.mean))) + theme_classic(base_family="Gill Sans")+ scale_y_continuous(name="", breaks=c(58,62,66,70,74,78,82,86,90,94,98,102, 106), labels=newsitlabs, limits=c(57, 108),) + scale_x_continuous(name="\n(B) Sweden \n", breaks=seq(1800, 2020, by = 20)) +
    scale_color_manual(name="Income group & coverage", 
                       labels = fig1labs,
                        values = c("black", "black", "#C10534", "#C10534", "#3E647D", "#3E647D")) + 
    scale_linetype_manual(name="Income group & coverage", 
                     labels = fig1labs,  
                     values=c("solid", "dashed", "solid", "dashed", "solid", "dashed")) + 
  theme(legend.position="bottom", legend.title = element_text(size = 14), legend.text = element_text(size = 14), axis.text.y = element_text(size = 16), axis.text.x = element_text(size = 14), axis.title.x = element_text(size = 22)) + theme(axis.text.y=element_blank())

ggarrange(ENG_1a, SWE_1a, common.legend = TRUE, legend = "bottom", nrow=1, ncol = 2, widths = c(1, 0.65))

ggsave('Figure_1.png', scale = 1.5, width = 24, height = 10,  units = "cm") 

```

## Figure 8

```{r finding 3A, echo=FALSE, warning=FALSE}
fig8breaks <-c(1,2,3,4,5,6,7,8,9,10,11,12,13)

#Station - Met at the official's station
df2<-mutate(df2, station = ifelse(place=="3", 2, ifelse(grepl("3", place), 1, ifelse(!is.na(df2$some), 0, NA))))
plotdata<-summaryBy(station ~ cname + year + newsit, df2, FUN=c(mean), na.rm=TRUE)
plotdata$station.mean[is.na(plotdata$station.mean)] <- "3"
plotdata<-mutate(plotdata, station = ifelse(station.mean == 0, 0, ifelse(station.mean > 0 & station.mean < 2, 1, ifelse(station.mean == 2, 2, 3))))
plotdata$station <- factor(plotdata$station,levels = c(2,1,0,3), labels = c("At the official's office or station", "Sometimes at the office/station", "Elsewhere", ""))

ENG_3a <- ggplot(subset(plotdata, cname=="England"),
    aes(year, newsit, group=newsit, alpha=station)) + geom_line(size=2) +
  theme_classic(base_family="Gill Sans") + 
  scale_y_continuous(name="", breaks=fig8breaks, labels=newsitlabs, limits=c(1, 13),) + 
  scale_x_continuous(name="\n(A) England \n", breaks=seq(1800, 2020, by = 20)) + 
  scale_alpha_manual("Location of interaction", values = c(1,0.4, 0.1, 0)) + theme(legend.position="bottom", legend.title = element_text(size = 14), legend.text = element_text(size = 14), legend.box.spacing= unit(.05, "cm"), axis.text.y = element_text(size = 16), axis.text.x = element_text(size = 14), axis.title.x = element_text(size = 22)) + guides(alpha = guide_legend(title.position = "top", title.hjust =0.5))

SWE_3a <- ggplot(subset(plotdata, cname=="Sweden"),
    aes(year, newsit, group=newsit, alpha=station)) + 
  geom_line(size=2) + theme_classic(base_family="Gill Sans") + 
  scale_y_continuous(name="", breaks=fig8breaks, labels=newsitlabs, limits=c(1, 13),) + 
  scale_x_continuous(name="\n(B) Sweden \n", breaks=seq(1800, 2020, by = 20)) + 
  scale_alpha_manual("Location of interaction", values = c(1,0.4, 0.1, 0)) + theme(legend.position="bottom", legend.title = element_text(size = 14), legend.text = element_text(size = 14), legend.box.spacing= unit(.05, "cm"), axis.text.y = element_text(size = 16), axis.text.x = element_text(size = 14), axis.title.x = element_text(size = 22)) + theme(axis.text.y=element_blank()) + guides(alpha = guide_legend(title.position = "top", title.hjust =0.5))

ggarrange(ENG_3a, SWE_3a, common.legend = TRUE, legend = "bottom", nrow=1, ncol = 2, widths = c(1, 0.65))

ggsave('Figure_8.png', scale = 1.5, width = 24, height = 10,  units = "cm") 

```


